package com.mod.reanimator 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.core.Project;
	import com.calefk.modules.math.Vector2D;
	import com.calefk.util.helper;
	import com.mod.data.dtLevel;
	import com.mod.enemies.EnemyManager;
	import com.calefk.util.IntUtil;
	import com.mod.Global;
	import com.mod.ModProject;
	import com.mod.PMGame;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	/**
	 * ...
	 * @author authorX
	 */
	public class World extends EntitySimple 
	{
		public var hearth:MovieClip;
		private var numberVillagers:int = 7;
		private var numberGuardians:int = 7;
		public var arrayVillagers:Array;
		private var _player:Player;
		private var tarjetVillager:BaseVillager;
		
		public function get villagers():Array { return arrayVillagers; }
		
		public function World(canvas:MovieClip, nameClip:String=null, px:Number=0, py:Number=0, project:Project=null, enemyManager:EnemyManager = null) 
		{
			super(canvas, nameClip, px, py, project);
			hearth = mClip.hearth_mc;

			
			arrayVillagers = createVillagers(enemyManager);
			canvas.stage.addEventListener(MouseEvent.CLICK, clickPayer);
			canvas.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			//createWarrior();

		}
		

		
		private function clickPayer(e:MouseEvent):void 
		{
			
		}
		
		private function mouseUp(e:MouseEvent):void 
		{
			tarjetVillager.changeBarState();
			tarjetVillager.deactivateMyBar();
			mCanvas.stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
			mCanvas.stage.addEventListener(MouseEvent.CLICK, clickPayer);
			mCanvas.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			if (tarjetVillager.currentState == BaseVillager.EXPLODING) {
				Global.playSound("snd_death_2", 1, 5);
			}
			tarjetVillager = null;
			Global.stopSound("snd_reanimate");
			player.switchState(Player.ENDINVOKING);
		}
		
		private function mouseDown(e:MouseEvent):void 
		{
			if (!tarjetVillager)
			{
				for each(var tempVillager:BaseVillager in arrayVillagers)
				{
					if (player.verifyCircleCollision(tempVillager.clip))
					{
						if (tempVillager.clip.hitTestPoint(e.stageX, e.stageY))
						{
							trace("hizo click villager dentro del circulo en: " + e.stageX + "," + e.stageY);
							if (tempVillager.isDead())
							{
								Global.playSound("snd_reanimate",int.MAX_VALUE,20);
								//mCanvas.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
								tarjetVillager = tempVillager;
								tarjetVillager.activateMyBar();
								mCanvas.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp, false, 0, true);
								mCanvas.stage.removeEventListener(MouseEvent.CLICK, clickPayer);
								mCanvas.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
								player.switchState(Player.INVOKING);
								return;
							}
						}
					}
				}
			}
		}
		
		private function createVillagers(enemyManager:EnemyManager):Array {
			var dataLevel:dtLevel = Global.dataGame.levels[Global.currentLevel];
			var tempArray:Array = [];
			if (dataLevel) {
				/* Generar una posición que no colision con el heart*/
				var tempVillager:BaseVillager;
				
				var i:int;
				var pos:Vector2D;
				for (i = 0; i < dataLevel.numVillagers; i++ )
				{
					pos = generatePosition();
					tempVillager = new villager(mCanvas, "aldeano", pos.x, pos.y , null,enemyManager);
					tempArray.push(tempVillager);
				}
				for (i = 0; i < dataLevel.numGuardians; i++ )
				{
					pos = generatePosition();
					tempVillager = new Guardian(mCanvas, "guardian", pos.x , pos.y , null,enemyManager);
					tempArray.push(tempVillager);
				}
				
			}
			return tempArray;
			
			/*
			var tempVillager:BaseVillager;
			var tempArray:Array = [];
			var i:int;
			for (i = 0; i < numberVillagers; i++ )
			{
				tempVillager = new villager(mCanvas, "aldeano", helper.randomInterval(150, 600), helper.randomInterval(40, 440), null,enemyManager);
				tempArray.push(tempVillager);
			}
			for (i = 0; i < numberGuardians; i++ )
			{
				tempVillager = new Guardian(mCanvas, "guardian", helper.randomInterval(150, 600), helper.randomInterval(40, 440), null,enemyManager);
				tempArray.push(tempVillager);
			}
			return tempArray;
			
			*/
		}
		
		private function generatePosition():Vector2D  {
			var gx:Number;
			var gy:Number;
			
			while (true) {
				gx = helper.randomInterval(150, 600);
				gy = helper.randomInterval(70 , 440);
				
				if (mClip.heart_skin) {
					if (!mClip.heart_skin.hitTestPoint(gx , gy , true)) {
						break;
					}
				}
			}
			return new Vector2D(gx  , gy);
		}
		
		public override function update(dt:int):void
		{
			/* update villagers */
			for (var k:int = 0 ; k <  arrayVillagers.length; k++ ) {
				var tempVillager:BaseVillager = arrayVillagers[k];
				if (tempVillager.toClean) {
					tempVillager.clean();
					arrayVillagers.splice(k , 1);
				} else {
					tempVillager.update(dt);
				}
			}
			
			if (tarjetVillager) {
				if (!tarjetVillager.toClean){
					tarjetVillager.updateBar(dt);
				}
			}

		}
		
		override public function clean():void {
			mCanvas.stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
			mCanvas.stage.removeEventListener(MouseEvent.CLICK,clickPayer);
			mCanvas.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			super.clean();
		}
		
		
		public function get player():Player 
		{
			return _player;
		}
		
		public function set player(value:Player):void 
		{
			_player = value;
		}	
	}
}